package com.suxin.plugin.pf4j;

import com.suxin.plugin.intergration.manager.PluginWrapper;

import java.lang.reflect.Constructor;

/**
 * @author Tang
 * @classname DefaultPluginFactory
 * @description [  ]
 * @date 2022/7/20 22:06
 */
public class DefaultPluginFactory implements com.suxin.plugin.pf4j.PluginFactory {

    @Override
    public BasePlugin create(PluginWrapper pluginWrapper) {
        String pluginClassName = pluginWrapper.getPluginDescriptor().getPluginClass();
        try {
            // 加载插件配置类入口
            Class<?> pluginClass = pluginWrapper.getClassLoader().loadClass(pluginClassName);
            Constructor<?> constructor = pluginClass.getConstructor(pluginWrapper.getClass());
            return (BasePlugin) constructor.newInstance(pluginWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
